package com.foreknow.filter;




import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;



public class CharacterEncodingFilter implements Filter {
	private FilterConfig config;
	private String encoding = "ISO8859-1";

	public void destroy() {
		config = null;
	}
	//核心的拦截处理方法
	public void doFilter(ServletRequest request, ServletResponse response,
						 FilterChain chain) throws IOException, ServletException {

		request.setCharacterEncoding(encoding);
		//如果还有其它的过滤器，那就就交给下一个过滤器，如果没有就将请求交给servlet
		// 把请求传回过滤链
		chain.doFilter(request, response);
	}

	public void init(FilterConfig config) throws ServletException {
		this.config = config;
		//获取初始化参数web.xml
		//<init-param>
		//		<param-name>encoding</param-name>
		//		<param-value>UTF-8</param-value>
		//</init-param>
		encoding = config.getInitParameter("encoding");
		System.out.println(encoding);
	}
}
